﻿using MeterialManager.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MeterialManager
{
    public partial class FrmMeterialManager : Form
    {
        
        DelBindDgv delBindDgv;
        public FrmMeterialManager()
        {
            InitializeComponent();
        }

        private void FrmMeterialManager_Load(object sender, EventArgs e)
        {
            BindCbx();
            BindDgv();
            delBindDgv = BindDgv;
        }

        public void BindCbx()
        {
            List<Category> categories = new List<Category>();
            categories = Category.GetListCategory();
            categories.Insert(0, new Category()
            {
                CategoryId = 0,
                CategoryCode = "",
                CategoryName = "-查询所有-"
            });
            cbxCategoryName.DataSource = categories;
            cbxCategoryName.DisplayMember = "CategoryName";
            cbxCategoryName.ValueMember = "CategoryId";
        }
        public void BindDgv()
        {
            string meterialCode = tbMeterialCode.Text.Trim();
            string meterialName = tbMeterialName.Text.Trim();
            int CategoryId = (int)cbxCategoryName.SelectedValue;
            string unit = tbUnit.Text.Trim();
            dgv_Category.DataSource = Category.GetListCategory();
            List<MeterialCategory> meterialCategories = MeterialCategory.ListAll();
            if (CategoryId == 0)
            {
                dgvMeterialCategory.DataSource = meterialCategories.FindAll(m => m.MeterialCode.Contains(meterialCode) && m.MeterialName.Contains(meterialName) && m.Unit.Contains(unit));
            }
            else
            {
                dgvMeterialCategory.DataSource = meterialCategories.FindAll(m => m.CategoryId == CategoryId && m.MeterialCode.Contains(meterialCode) && m.MeterialName.Contains(meterialName) && m.Unit.Contains(unit));
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            BindDgv();
        }

        private void dgvMeterialCategory_MouseDown(object sender, MouseEventArgs e)
        {

            if (e.Button == MouseButtons.Right)
            {
                tsmAdd.Visible = true;
                tsmEdit.Visible = false;
                tsmDel.Visible = false;
            }
        }
        private void dgvMeterialCategory_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (e.RowIndex > -1)
                {
                    dgvMeterialCategory.Rows[e.RowIndex].Selected = true;
                    tsmAdd.Visible = false;
                    tsmEdit.Visible = true;
                    tsmDel.Visible = true;
                }
            }
        }

        private void tsmAdd_Click(object sender, EventArgs e)
        {
            int categoryId = (int)cbxCategoryName.SelectedValue;
            if (categoryId == 0)
            {
                FrmSetMeterial frmSetMeterial = new FrmSetMeterial(delBindDgv);
                frmSetMeterial.ShowDialog();
            }
            else
            {
                FrmSetMeterial frmSetMeterial = new FrmSetMeterial(delBindDgv, categoryId);
                frmSetMeterial.ShowDialog();
            }
        }

        private void tsmEdit_Click(object sender, EventArgs e)
        {
            int categoryId = (int)cbxCategoryName.SelectedValue;
            int id = (int)dgvMeterialCategory.SelectedRows[0].Cells["Id"].Value;
            FrmSetMeterial frmSetMeterial = new FrmSetMeterial(delBindDgv,id,categoryId);
            frmSetMeterial.ShowDialog();
        }

        private void tsmDel_Click(object sender, EventArgs e)
        {
            int id = (int)dgvMeterialCategory.SelectedRows[0].Cells["Id"].Value;
            if (MessageBox.Show("确认删除？", "是否删除？", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK)
            {
                Meterial.Delete(id);
            };
        }


        private void FrmMeterialManager_Enter(object sender, EventArgs e)
        {
            BindCbx();
            BindDgv();
            SelectCategory();
        }


        private void dgv_Category_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                dgv_Category.Rows[e.RowIndex].Selected = true;
            }
            SelectCategory();
        }

        private void SelectCategory()
        {
            int id = (int)dgv_Category.SelectedRows[0].Cells["CategoryId"].Value;
            List<MeterialCategory> meterialCategories = MeterialCategory.ListAll();
            dgvMeterialCategory.DataSource = meterialCategories.FindAll(m => m.CategoryId == id);
        }

        private void FrmMeterialManager_Activated(object sender, EventArgs e)
        {
            dgv_Category.Focus();
        }
    }
}
